<?php

namespace app\common\service;

/**
 * 全局变量服务
 */
class GlobalsVariableService
{
    /**
     * collection
     * @var array
     */
    private static array $collection;

    /*
     * internalVariable
     */
    private static array $internalVariable = [
        'appId',
        'appSign',
        'appSafeCode',
        'appVersion',
        'appPlatform',
    ];

    /**
     * set
     * @param string $key
     * @param mixed $value
     */
    public static function set(string $key, $value)
    {
        if (static::isInternalVariable($key)) {
            $key = strtolower($key);
        }
        static::$collection[$key] = $value;
    }

    /**
     * get
     * @param string $key
     * @return mixed
     */
    public static function get(string $key)
    {
        if (static::isInternalVariable($key)) {
            $key = strtolower($key);
        }
        return static::$collection[$key] ?? false;
    }

    /**
     * isInternalVariable
     * @param $key
     * @return bool
     */
    public static function isInternalVariable($key): bool
    {
        foreach (static::$internalVariable as $name) {
            if (strtolower($key) == strtolower($name)) {
                return true;
            }
        }
        return false;
    }
}